package com.example.mall.web.controller;

import com.example.mall.domain.CouponTemplate;
import com.example.mall.dto.CouponTemplateDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.CouponTemplateQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ICouponTemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/couponTemplate")
public class CouponTemplateController {

    @Autowired
    public ICouponTemplateService couponTemplateService;

    /**
     * 分页查询优惠券模板表信息
     * @param couponTemplateQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询优惠券模板表信息",notes = "分页查询优惠券模板表信息",httpMethod = "GET")
    public JSONResult queryPage(CouponTemplateQuery couponTemplateQuery){
        PageDTO<CouponTemplateDto> couponTemplateDtoPageDTO = couponTemplateService.queryPage(couponTemplateQuery);
        return JSONResult.success("查询成功",couponTemplateDtoPageDTO);
    }

    /**
     * 根据id查询优惠券模板表信息
     * @param templateId
     * @return
     */
    @GetMapping("query/{templateId}")
    @ApiOperation(value = "根据id查询优惠券模板表信息",notes = "根据id查询优惠券模板表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("templateId")Long templateId){
        //1.根据id查询用户信息
        if (templateId == null){
            throw new GlobalCustomerException(ErrorEnumCode.TEMPLATEID_NOT_ID);
        }
        CouponTemplate couponTemplate = couponTemplateService.getById(templateId);
        return JSONResult.success("查询成功",couponTemplate);
    }

    /**
     * 根据id查询优惠券模板表信息
     * @param templateId
     * @return
     */
    @GetMapping("queryCouponTemplateById/{templateId}")
    @ApiOperation(value = "根据id查询优惠券模板表信息",notes = "根据id查询优惠券模板表信息",httpMethod = "GET")
    public JSONResult queryCouponTemplateById(@PathVariable("templateId")Long templateId){
        //1.根据id查询用户信息
        if (templateId == null){
            throw new GlobalCustomerException(ErrorEnumCode.TEMPLATEID_NOT_ID);
        }
        CouponTemplate couponTemplate = couponTemplateService.queryCouponTemplateById(templateId);
        return JSONResult.success("查询成功",couponTemplate);
    }

    /**
     * 查询全部优惠券模板表信息
     * @param
     * @return
     */
    @GetMapping("queryCouponTemplateList")
    @ApiOperation(value = "查询全部优惠券模板表信息",notes = "查询全部优惠券模板表信息",httpMethod = "GET")
    public JSONResult queryCouponTemplateList(){
        List<CouponTemplate> logins = couponTemplateService.queryCouponTemplateList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增优惠券模板表
     * @param couponTemplateDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增优惠券模板表",notes = "新增优惠券模板表",httpMethod = "POST")
    public JSONResult insert(@RequestBody CouponTemplateDto couponTemplateDto){
        couponTemplateService.insert(couponTemplateDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改优惠券模板表信息
     * @param couponTemplateDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改优惠券模板表信息",notes = "修改优惠券模板表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody CouponTemplateDto couponTemplateDto){
        couponTemplateService.update(couponTemplateDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除优惠券模板表信息
     * @param templateId
     * @return
     */
    @PostMapping("delete/{templateId}")
    @ApiOperation(value = "删除优惠券模板表信息",notes = "删除优惠券模板表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("templateId") Long templateId) {
        couponTemplateService.delete(templateId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除优惠券模板表信息
     * @param templateIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除优惠券模板表信息",notes = "批量删除优惠券模板表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> templateIds) {
        couponTemplateService.deleteBatch(templateIds);
        return JSONResult.success("批量删除成功");
    }
}
